const app = getApp();
const moment = require('../../utils/moment-with-locales.min.js');
moment.locale('zh-cn');
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    loadingStatus: true,
    dispatch: null
  },
  onLoad(options) {
    if (app.globalData.userInfo) {
      var that = this;
      const param = {
        sessionKey: app.globalData.sessionId,
        id: options.id
      };
      wx.request({
        url: app.url.getDetail,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: param,
        success: function (res) {
          const data = res.data;
          const dispatch = data.data;
          if (data.result == "true") {
            that.formatCountDown(dispatch);
            that.setData({
              loadingStatus: false,
              dispatch: dispatch
            });
          } else {
            wx.showModal({
              content: data.message,
              showCancel: false,
              success: function (res) { }
            });
            that.setData({
              loadingStatus: false,
              dispatch: null
            });
          }
        },
        fail: res => {
          wx.showModal({
            content: "获取数据失败！",
            showCancel: false,
            success: function (res) { }
          });
        }
      });
    }
  },
  doFinish(e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提醒',
      content: '确定完成该接单吗？',
      confirmText: '完成',
      confirmColor: '#39b54a',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍后...',
            mask: true
          });
          const param = {
            id: id,
            type: "3",
            sessionKey: app.globalData.sessionId
          };
          var that = this;
          wx.request({
            url: app.url.doOpration,
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: param,
            success: function (res) {
              wx.hideLoading();
              const data = res.data;
              if (data.result == 'true') {
                wx.showToast({
                  title: data.message,
                  icon: 'success',
                  duration: 2000,
                  success(){
                    wx.navigateBack({
                      delta: 1
                    });
                  }
                });
              } else {
                wx.showToast({
                  title: data.message,
                  image: '/images/error1.png',
                  duration: 2000
                });
              }
            },
            fail: res => {
              wx.showModal({
                content: "操作失败，请重试！",
                showCancel: false,
                success: function (res) { }
              });
            }
          });
        } else if (res.cancel) { }
      }
    });
  },
  formatCountDown(obj) {
      obj.fromNow = moment(obj.orderTime).fromNow();
      obj.countDown = this.getCountDown(obj.orderTime, obj.delayMinute);
      if (!obj.countDown) {
        obj.overTime = true;
      }
  },
  getCountDown(dateStr, delayMinute) {
    const diff = moment(dateStr).add(delayMinute, 'minute').diff(moment(), 'seconds');
    if (diff <= 0) {
      return null;
    }
    var diffMinute = parseInt(diff / 60);
    var diffSecond = diff % 60
    var countDownStr = (diffMinute > 0 ? (diffMinute + '分') : '') + diffSecond + '秒';
    return countDownStr;
  },
})